package com.unknown.wificontrol.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.listener.HXItemSelectListener;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BottomPopItemView extends Dialog implements
        android.view.View.OnClickListener {

    public BottomPopItemView(Context context) {
        super(context, R.style.Translucent_NoTitle);
        init(context);
    }

    private WheelView wheelView;
    private HXItemSelectListener listener;
    private List<String> mDatas;

    private void init(Context context) {
        mDatas = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(context);
        View bgView = inflater.inflate(R.layout.pop_layout, null);
        wheelView = bgView.findViewById(R.id.wheelview);
        Button btnOk = (Button) bgView.findViewById(R.id.btn_ok);
        Button btnCancel = (Button) bgView.findViewById(R.id.btn_cancel);
        setContentView(bgView);
        btnCancel.setOnClickListener(this);
        btnOk.setOnClickListener(this);
    }

    public void show(String defaultText, String[] items,
                     HXItemSelectListener listener) {
        List<String> lists = Arrays.asList(items);
        show(defaultText, lists, listener);
    }


    public void show(String defaultText, List<String> lists,
                     HXItemSelectListener listener) {
        mDatas.clear();
        mDatas.addAll(lists);
        wheelView.setData(lists);
        int selectIndex = 0;

        if (!TextUtils.isEmpty(defaultText)) {
            int index = lists.indexOf(defaultText);
            if (index >= 0) {
                selectIndex = index;
            }
        }

        wheelView.setSelectedItemPosition(selectIndex);

        this.listener = listener;
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.AnimBottom);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        show();
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.btn_ok: {
                if (listener != null) {
                    listener.onSelected(wheelView.getSelectedItemPosition());
                }
                dismiss();
            }

            break;
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

}
